package cn.ibizlab.codegen.groovy.support

import net.ibizsys.model.IPSSystem
import net.ibizsys.model.dataentity.IPSDataEntity
import org.springframework.util.ObjectUtils
import org.springframework.util.StringUtils

class PSDataEntityExtension {

    static String getBaseEntityRuntime(IPSDataEntity iPSDataentity) {
        String baseRuntime = "GroovyDataEntityRuntime";

        if ("EXTENSION" == iPSDataentity.getPSSystemModule().getUtilType()) {
            String strTag = iPSDataentity.getDETag();
            if(!StringUtils.hasLength(strTag)) {
                strTag = iPSDataentity.getName();
            }

            //根据前缀建立运行时
            if(strTag.indexOf("EXTENSION") == 0) {
                return "ExtensionDataEntityRuntime";
            }
            if(strTag.indexOf("PS") == 0) {
                return "GroovyPSModelDERuntime";
            }

        }



        //全局实体Runtime
        IPSSystem iPSSystem = iPSDataentity.getParentPSModelObject(IPSSystem.class, true)
        if (iPSSystem) {
            def globalEntityRuntimePlugin = iPSSystem.getAllPSSysSFPlugins()?.find { 'GLOBAL_DATAENTITYRUNTIME' == it.getCodeName() }
            if (globalEntityRuntimePlugin) {
                baseRuntime = globalEntityRuntimePlugin.getRTObjectName()
                if ("net.ibizsys.central.plugin.util.dataentity.DynaDataEntityRuntime".equalsIgnoreCase(globalEntityRuntimePlugin.getRTObjectName()))
                    baseRuntime = "GroovyDynaDataEntityRuntime"
            }
        }

        //实体Runtime
        if (iPSDataentity.getPSSysSFPlugin()) {
            baseRuntime = iPSDataentity.getPSSysSFPlugin().getRTObjectName()
        }
        return baseRuntime
    }

    static boolean isAdvCustom(IPSDataEntity iPSDataentity) {
        return (!ObjectUtils.isEmpty(iPSDataentity.getAllPSDEMaps())) || iPSDataentity.getPSSysSFPlugin()
    }

    static String getAccessManager(IPSDataEntity iPSDataentity) {
        String baseRuntime = "net.ibizsys.central.cloud.core.dataentity.security.DataEntityAccessManager"

        //全局实体Runtime
        IPSSystem iPSSystem = iPSDataentity.getParentPSModelObject(IPSSystem.class, true)
        if (iPSSystem) {
            def global = iPSSystem.getAllPSSysSFPlugins()?.find { 'GLOBAL_ACCESSMANAGER' == it.getCodeName() }
            if (global) {
                baseRuntime = global.getRTObjectName()
            }
        }
        return baseRuntime
    }


}
